import remoteLoad from '@/util/remoteLoad'
const { AMapCDN, AMapUiCDN } = require('@/plugins/cdn')

export default {
  getUrlKey: function (name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || ['', ''])[1].replace(/\+/g, '%20')) || null
  }
  // deepFlatten: function (arr) {
  //   return [].concat(...arr.map(a => (Array.isArray(a) ? deepFlatten(a) : a)))
  // }
}
/**
 * 函数防抖
 * @param {Function} func
 * @param {number} delay
 * @param {boolean} immediate
 * @return {*}
 */

export function debounce (func, delay, immediate = false) {
  let timer
  let context = this
  return (...args) => {
    if (immediate) {
      func.apply(context, args)
      immediate = false
      return
    }
    clearTimeout(timer)
    timer = setTimeout(() => {
      func.apply(context, args)
    }, delay)
  }
}

/**
 * 获取geoJson数据  通过高德获取    递归获取区县geoJson
 * @param  {string} adcode  行政区code
 * @param  {string} childAdcode 区县级行政区code
 * @return {Array}
 */
export function getGeoJson (adcode, childAdcode = '') {
  return new Promise((resolve, reject) => {
    if (window.AMap && window.AMapUI) {
      insideFun(adcode, childAdcode)
    } else {
      remoteLoad(AMapCDN).then(() => {
        if (window.AMap) {
          remoteLoad(AMapUiCDN).then(() => {
            if (window.AMapUI) {
              insideFun(adcode, childAdcode)
            } else {
              console.error('AMapUI获取失败')
            }
          })
        } else {
          console.error('AMap获取失败')
        }
      })
    }
    function insideFun (adcode, childAdcode) {
      // eslint-disable-next-line
      AMapUI.loadUI(['geo/DistrictExplorer'], DistrictExplorer => {
        var districtExplorer = new DistrictExplorer()
        districtExplorer.loadAreaNode(adcode, function (error, areaNode) {
          if (error) {
            console.error(error)
            reject(error)
            return
          }
          let Json = areaNode.getSubFeatures()
          if (Json.length === 0) {
            let parent = areaNode._data.geoData.parent.properties.acroutes
            insideFun(parent[parent.length - 1], adcode)
            return
          }

          if (childAdcode) {
            Json = Json.filter(item => {
              return item.properties.adcode === childAdcode
            })
          }
          let mapJson = {
            features: Json
          }
          resolve(mapJson)
        })
      })
    }
  })
}

/**  地图数据  散点
 *  @param {string}  adcode  城市code
 *  @returns {Array}
 */
export function getPointChartData (adcode) {
  return new Promise((resolve, reject) => {
    getGeoJson(adcode)
      .then((res) => {
        const data = res.features
        const mapData = data.map((item) => {
          return {
            name: item.properties.name,
            value: [
              item.properties.center[0],
              item.properties.center[1],
              parseFloat((Math.random(0.1, 1) * 10).toFixed(2))
            ],
            adcode: item.properties.adcode,
            level: item.properties.level
          }
        })
        resolve({
          code: 200,
          data: mapData
        })
      })
      .catch((error) => {
        reject(error)
      })
  })
}
